﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.Web.Mvc;
using System.Web.Routing;
using JustinAngel.MetroBlog._Code;
using JustinAngel.MetroBlog.Controllers;
using JustinAngel.MetroBlog.Services.Data;

namespace JustinAngel.MetroBlog
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection table)
        {
            table.IgnoreRoute("{resource}.axd/{*pathInfo}");

            table.MapRoute("dynamicSidebar", "Content/Sidebar/Sidebar.dynamic.js"
                , new { Controller = "SidebarJavascript", Action = "Show" });
            
            table.MapRoute("Home", "", new { Controller = "Home", Action = "Show" });

            table.MapRoute("Sitemap", "Sitemap", new { Controller = "Sitemap", Action = "Get" });
            table.MapRoute("LoginLoad", "ManagementLogin", new { Controller = "Login", Action = "Load" });
            table.MapRoute("LoginSubmit", "ManagementLogin/Submit", new { Controller = "Login", Action = "Submit" });


            table.Add(new DynamicDataRoute("Management/{table}/{action}.aspx")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });

            table.MapRoute("MainFeed", "Feed", new { Controller = "Feed", Action = "GetMain", format = "rss" });
            table.MapRoute("MainFeedFormat", "Feed/{format}", new { Controller = "Feed", Action = "GetMain" });
            table.MapRoute("BlogFeed", "Feed/{format}/{username}", new { Controller = "Feed", Action = "GetBlog" });
            table.MapRoute("CategoryFeed", "Feed/{format}/{username}/{category}", new { Controller = "Feed", Action = "GetCategory" });


            table.MapRoute("Articles", "{PermLink}",
                new { Controller = "Article", Action = "Show" },
                new { PermLink = new ArticleConstraint() });

            table.MapRoute("SubmitComment", "{PermLink}/NewComment",
                new { Controller = "Article", Action = "NewComment" },
                new { PermLink = new ArticleConstraint() });

            table.MapRoute("Categories", "{link}",
                new { Controller = "Category", Action = "Show" },
                new { link = new CategoryConstraint() });
        }

        protected void Application_Start()
        {
            Initializer.RegisterIoC();

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        private static MetaModel s_defaultModel = null;
        public static MetaModel DefaultModel
        {
            get
            {
                if (s_defaultModel == null)
                {
                    s_defaultModel = new MetaModel();
                    s_defaultModel.DynamicDataFolderVirtualPath = "~/Management/DynamicData/";
                    s_defaultModel.RegisterContext(typeof(EfContext), new ContextConfiguration() { ScaffoldAllTables = true });
                }
                return s_defaultModel;
            }
        }
    }
}